import type {
  AddReq,
  DelReq,
  EditReq,
  InfoReq,
  UserPageRes,
} from '@fast-crud/fast-crud';

import type { AxiosRequestConfig } from '@vben/request';

import type { BaseRoleModel } from './model/baseRoleModel';

import type { PageParams, PageResult } from '#/api';

import { requestClient } from '#/api/request';
import { ServicePrefixEnum } from '#/enums/commonEnum';

const MODULAR = 'baseRole';

const ServicePrefix = ServicePrefixEnum.BASE;

export const BaseRoleConfig = {
  Save: {
    url: `${ServicePrefix}/${MODULAR}`,
    method: 'POST',
  } as AxiosRequestConfig,
  Update: {
    url: `${ServicePrefix}/${MODULAR}`,
    method: 'PUT',
  } as AxiosRequestConfig,
};

export namespace BaseRoleApi {
  /**
   * 根据条件查询分页数据
   */
  export async function page(
    params: PageParams<BaseRoleModel.BaseRolePageQuery>,
  ) {
    return requestClient.post<PageResult<BaseRoleModel.BaseRoleResultVO>>(
      `${ServicePrefix}/${MODULAR}/page`,
      params,
    );
  }
  /**
   * 根据id查询单条数据
   */
  export async function get(id: string) {
    return requestClient.get<BaseRoleModel.BaseRoleResultVO>(
      `${ServicePrefix}/${MODULAR}/${id}`,
    );
  }

  export async function findEmployeeIdByRoleId(roleId: string) {
    return requestClient.get<string[]>(
      `${ServicePrefix}/${MODULAR}/employeeList`,
      { params: { roleId } },
    );
  }

  export async function findResourceDataScopeIdByRoleId(roleId: string) {
    return requestClient.get<Record<string, string[]>>(
      `${ServicePrefix}/${MODULAR}/findResourceDataScopeIdByRoleId`,
      { params: { roleId } },
    );
  }

  export async function findResourceIdByRoleId(roleId: string) {
    return requestClient.get<Record<string, string[]>>(
      `${ServicePrefix}/${MODULAR}/resourceList`,
      { params: { roleId } },
    );
  }

  /**
   * 保存
   */
  export async function save(params: BaseRoleModel.BaseRoleSaveVO) {
    return requestClient.post<BaseRoleModel.BaseRoleResultVO>(
      BaseRoleConfig.Save.url as string,
      params,
    );
  }
  //
  export async function saveRoleEmployee(
    params: BaseRoleModel.RoleEmployeeSaveVO,
  ) {
    return requestClient.post<string[]>(
      `${ServicePrefix}/${MODULAR}/roleEmployee`,
      params,
    );
  }

  export async function saveRoleResource(
    params: BaseRoleModel.BaseRoleResourceRelSaveVO,
  ) {
    return requestClient.post<boolean>(
      `${ServicePrefix}/${MODULAR}/roleResource`,
      params,
    );
  }

  /**
   * 修改
   */
  export async function update(params: BaseRoleModel.BaseRoleUpdateVO) {
    return requestClient.put<BaseRoleModel.BaseRoleResultVO>(
      BaseRoleConfig.Update.url as string,
      params,
    );
  }

  /**
   * 删除
   */
  export async function remove(data: string[]) {
    return requestClient.delete<boolean>(`${ServicePrefix}/${MODULAR}`, {
      data,
    });
  }

  // fast-crud 需要的接口
  export const pageRequest = async (
    pageQuery: PageParams<BaseRoleModel.BaseRolePageQuery>,
  ): Promise<UserPageRes> => {
    return await page(pageQuery);
  };

  export const infoRequest = async (ctx: InfoReq): Promise<UserPageRes> => {
    const { row } = ctx;
    // 请求后台查询数据
    return row;
  };

  export const addRequest = async (req: AddReq) => {
    const { form } = req;
    return await save(form);
  };

  export const editRequest = async (ctx: EditReq) => {
    const { form } = ctx;
    return await update(form);
  };

  export const delRequest = async (ctx: DelReq) => {
    const { row } = ctx;
    return await remove([row.id]);
  };
}
